package com.rs.portal.doctor.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.commons.RowImpl;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.util.Pagination;
import com.rs.util.Utils;

public class DoctorAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    /**
     * 专家查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchDoc(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);

        // 取得总记录数
        int pageIndex = 1;
        if (param.getInt("pageIndex") != 0)
        {
            pageIndex = param.getInt("pageIndex");
        }
        param.put("pageIndex", pageIndex);
        param.put("pageSize", RsConstant.PAGESIZE);
        Object[] result = this.getDoctorDelegate().getDocList(param);
        List list = (List) result[0];
        Integer totalCount = (Integer) result[1];
        // 处理分页
        JSONObject obj = new JSONObject();
        obj.put("totalCount", totalCount);
        JSONArray array = JSONArray.fromObject(list);
        array.add(0, obj);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(array.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    /**
     * 获取分页
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView getPageHtml(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);

        // 取得总记录数
        int pageIndex = 1;
        if (param.getInt("pageIndex") != 0)
        {
            pageIndex = param.getInt("pageIndex");
        }
        // 处理分页
        Pagination pagination = new Pagination();
        String pageHtml = pagination.getGoogleToolsBar(param.getInt("totalCount"), "pagination", pageIndex, RsConstant.PAGESIZE);

        response.setContentType("text/html;charset=UTF-8");
        System.out.println(pageHtml);
        response.getWriter().write(pageHtml);
        response.getWriter().flush();
        response.getWriter().close();
        return null;
        // return new ModelAndView("doctor_search_result", attrs);
    }

    /**
     * 专家详情查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchDocInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        Row docInfo = this.getDoctorDelegate().getDocDetail(param);
        List docSchList = this.getDoctorDelegate().getDocSchList(param);
        attrs.put("docInfo", docInfo);
        attrs.put("docSchList", docSchList);
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("doctor", attrs);
    }

    /**
     * 医院详情查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchHosInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        Row hosInfo = this.getDoctorDelegate().getHospitalInfo(param);

        attrs.put("hosInfo", hosInfo); // 这一句相当于request.setAttribute("list",list);
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("hospital_detail", attrs);
    }

    /**
     * 科室详情查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchSecInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        Row secInfo = this.getDoctorDelegate().getSectionInfo(param);

        attrs.put("secInfo", secInfo); // 这一句相当于request.setAttribute("list",list);
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("section_detail", attrs);
    }

    /**
     * 获取医院列表
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initHosList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        List hosList = this.getCommonDelegate().getAllHospitalList();
        JSONArray array = JSONArray.fromObject(hosList);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(array.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    public ModelAndView getHosList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        List hosList = this.getCommonDelegate().getAllHospitalList();
        Map attrs = new HashMap();
        attrs.put("hosList", hosList);
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("doctor_search", attrs);
    }

    /**
     * 获取医院对应的科室列表
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initSecList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        List secList = this.getCommonDelegate().getAllSectionWithParentList(param);
        JSONArray array = JSONArray.fromObject(secList);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(array.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    /**
     * 初始化日期选择列表
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initDateList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        // 创建日期列表
        List dateList = new ArrayList();
        Row row = null;
        for (int i = 1; i < RsConstant.PRE_SCHEDULE_DAYS; i++)
        {
            row = new RowImpl();
            row.addColumn("key", Utils.getCurAfterDate("yyyy-MM-dd", i));
            row.addColumn("val", Utils.getCurAfterDate("yyyy-MM-dd", i));
            dateList.add(row);
        }
        JSONArray array = JSONArray.fromObject(dateList);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(array.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }
}
